import 'package:getx_demo/pages/home/state.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomeController extends GetxController {
  HomeController();

  var state = HomeState();

  _initData() {
    update(["home"]);
  }

  void onTap() {}

  void changeSelectedIdx(int idx) {
    state.currentIdx = idx;
    _initData();
  }

  List get currentDataSource {
    var datas = state.dataSource[state.currentIdx] ?? [];
    return datas;
  }

  RefreshController get currentRefreshConntroller {
    return state.refreshs[state.currentIdx];
  }

  int get currentPageIdx {
    return state.pageData[state.currentIdx];
  }

  RefreshController refreshConntrollerAt(int idx) {
    return state.refreshs[idx];
  }

  void onRefreshData() {
    // final refreshC = refreshConntrollerAt(state.currentIdx);
    state.updatePage(1);
    _requestData(true);
  }

  void onLoadingData() {
    state.updatePage(currentPageIdx + 1);
    _requestData(false);
  }

  Future<void> _requestData(bool isRefresh) async {
    debugPrint('--->获取列表稿件数据-->');
    var param = <String, dynamic>{};
    int pageSize = 20;
    String syStatus = '稿件';
    // if (vmtype == DraftListVMType.materialCenter) {
    //   syStatus = '素材';
    // }
    // param['syVerifyStatus'] = status == DraftStatus.none ? '' : status.title;
    param['syVerifyStatus'] = '';
    param['syStatus'] = syStatus;
    param['page'] = currentPageIdx;
    param['size'] = pageSize;
    param['irTitle'] = '';
    // ApiCallResult result = await APICaller.call(
    //   ERDSApiPath.manuscriptsList,
    //   params: param,
    // );
    // bool isNoMoreData = false;
    // if (result.success) {
    //   int totalRecords = (result.data['totalRecords'] ?? 0) as int;
    //   var pageData = (result.data['pageData'] ?? []) as List;
    //   var datas = pageData.map((e) => ManuscriptModel.fromJson(e)).toList();
    //   if (isRefresh) {
    //     dataSource.clear();
    //   }
    //   dataSource.addAll(datas);
    //   isNoMoreData = datas.isEmpty || (_pageIdx * pageSize) >= totalRecords;
    //   requestCallback();
    // } else {
    //   EasyLoading.showError(result.errorInfo);
    // }
    // if (isRefresh) {
    //   refreshController.refreshCompleted();
    // } else {
    //   refreshController.loadComplete();
    // }
    // if (isNoMoreData) {
    //   refreshController.loadNoData();
    // } else {
    //   refreshController.resetNoData();
    // }
  }

  // @override
  // void onInit() {
  //   super.onInit();
  // }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
